<?php

class ControllerAdmin_Modules extends ControllerAdmin_Base
{
	public function __construct($section) {		
		$this->section      = "modules";
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."modules";
	}
	
	public function getFirstPage()
	{
		/* Изменение порядка и проверка на удаление */
		if($_POST['subaction'] == 'updateitems') {			
			$this->confirm = functions::updateItems($_POST, $this->table_module, "por");
		}
		/* Удаление записей */
		elseif($_POST['id_delete']) {
			$this->confirm = functions::deleteItems($_POST, $this->table_module);
		}
		
		/* Список модулей */
		$r =& Registry::get("MDB")->query("SELECT * FROM `".$this->table_module."` ORDER BY `Por`;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if($r->numRows()) {
			while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$modules_list[] = $p;
			}
		}		
		Registry::get("smarty")->assign(array(
			'CFG_FIELD'  => array(						
				'edit'   => true,
				'por'    => true,
				'delete' => true,			 
				'field_cfg' => array(
					array(
						'name'      => 'Название раздела',
						'fieldname' => 'name',
						'width'     => '33%',
						'link'      => true,			
						'attr'      => false
					),
					array(
						'name'      => 'Модуль',
						'fieldname' => 'controller',
						'width'     => '33%',
						'link'      => true,			
						'attr'      => false
					),
					array(
						'name'      => 'Таблица',
						'fieldname' => 'db_name',
						'width'     => '33%',
						'link'      => true,			
						'attr'      => false
					)
				),
				"aoData" => array(
					"bSortable" => array(
						"false",						
						"false",
						"false",
						"null",
						"null",
						"null"
					)
				)
			),
			'TBODY_TD'        => $modules_list,
			'ACT_NAME'        => "modules",
			'CONFIRM'         => $this->confirm
		));		
		return Registry::get("smarty")->fetch(TEMPLATE_ADMIN.$this->common_vars["controller_template"], Registry::get("cache_id"));
	}
	
	public function init(){}	
	public function addToTable()
	{
		if(!empty($_POST['type']))
		{				
			extract($_POST);			
			if ($_POST['type'] == 'add') {
				eval(functions::insert_sql($this->table_module, 'sql'));
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
			elseif($_POST['type'] == 'editt') {
				eval(functions::update_sql($this->table_module, 'sql', 'id', $_POST['id']));				
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
		}
	}
	
    public function out()
	{
		self::addToTable();
		
		if(empty($_GET['type'])) {
			$this->action = '<div class="addbuttons"><a href="?m=modules&type=add" class="ui_wo_buttons ui-state-default ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Добавить модуль</a></div>';			
			return self::getFirstPage();
		}		
		else
		{
			if($_GET['type'] == 'edit')
			{							   
				$r =& Registry::get("MDB")->query("SELECT * FROM `".$this->table_module."` WHERE `ID`='".$_GET['id']."' LIMIT 1;");
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
				if($r->numRows()) {
					$Field = $r->fetchRow(MDB2_FETCHMODE_ASSOC);
				}
			}			
			elseif($_GET['type'] == 'add' || empty($_GET['type'])) {
				$Field["por"] = functions::incrPor($this->table_module);			
			}
			
			if(!empty($_GET['id'])) {
				$id_action = $_GET['id'];
			}		

			Registry::get("smarty")->assign(array(
				'SECTION'   => $this->section,
				'Field'     => $Field
			));
			return Registry::get("smarty")->fetch(TEMPLATE_ADMIN.$this->common_vars["controller_template"], Registry::get("cache_id"));
		}		
    }
}

$controller = new ControllerAdmin_Modules($section);
$ob_admin_out = $controller->out();
$ob_admin_action = $controller->action;

?>